<?php
class EventManager_Validate_UniqueEmail extends Zend_Validate_Abstract{

	const EMAIL_EXISTS = 'emailExists';

	protected $_messageTemplates = array(
	self::EMAIL_EXISTS => 'Email "%value%" already exists in our system',
	);

	public function __construct(EventManager_Model_User $model){

		$this->_model = $model;

	}
	public function isValid($value,$context = null){

		$this->_setValue($value);
		$currentUser = isset($context['usr_id']) ? $this->_model->getUserById($context['usr_id']) : null;
		$user = $this->_model->getUserByEmail($value, $currentUser);
		if(null === $user){
			return true;
		}
		$this->_error(self::EMAIL_EXISTS);
		return false;
	}

}